package com.yuyou.fn.timer.task;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.portal.entity.OuterOrder;
import com.yuyou.fn.portal.service.IOuterOrderService;
import com.yuyou.fn.settlement.service.IPayOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 检测撤销超时的订单
 * Created by wens on 2017/4/19.
 */
@JobHander(value="CancelOuterOrderTask")
@Component
public class CancelOuterOrderTask extends IJobHandler {


    private static final Logger log = LoggerFactory.getLogger(CancelOuterOrderTask.class);


    @Resource
    private IOuterOrderService outerOrderService;

    @Resource
    private IPayOrderService payOrderService;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        Calendar cal = Calendar. getInstance ();
        cal.add(Calendar.HOUR,-1);
        Date date=cal.getTime();
        List<OuterOrder> outerOrderList =outerOrderService.findByLessCreateTimeAndStatus(date,0);
        for(OuterOrder outerOrder : outerOrderList){
            try{
                if( !payOrderService.isPaid(outerOrder.getOuterOrderId())){
                    outerOrderService.cancelOuterOrder(outerOrder.getOuterOrderId(),4);
                }
            }catch (Exception e){
                XxlJobLogger.log("撤销订单"+ outerOrder.getOuterOrderId()+"失败" );
            }

        }
        return ReturnT.SUCCESS ;
    }

}
